using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    [ToolboxData("<{0}:FlashContentControlEdit runat=server></{0}:FlashContentControlEdit>")]
    public class FlashContentControlEdit : BaseContentControlEdit
    {
        private FlashContentControlProperties properties;
        public FlashContentControlProperties Properties
        {
            get
            {
                if (properties == null)
                    properties = new FlashContentControlProperties();
                return properties;
            }
        }

        private FileUpload fileUpload;
        private Label editWidthLbl;
        private TextBox editWidth;
        private RequiredFieldValidator editWidthReq;
        private CompareValidator editWidthType;
        private Label editHeightLbl;
        private TextBox editHeight;
        private RequiredFieldValidator editHeightReq;
        private CompareValidator editHeightType;
        private Label editMinVersionLbl;
        private TextBox editMinVersion;
        private RequiredFieldValidator editMinVersionReq;

        private PlaceHolder displayNotEmpty;
        private HyperLink displayLink;
        private Label displayDimentions;
        private Label displayEmpty;
        private Label displayMinVersion;

        protected override void CreateDisplayControls(PlaceHolder placholder)
        {
            displayNotEmpty = new PlaceHolder();
            placholder.Controls.Add(displayNotEmpty);

            displayLink = new HyperLink();
            displayLink.Text = "View Flash Movie";
            displayLink.Target = "_Blank";
            displayNotEmpty.Controls.Add(displayLink);

            displayNotEmpty.Controls.Add(new LiteralControl("<br/>"));

            displayDimentions = new Label();
            displayNotEmpty.Controls.Add(displayDimentions);

            displayNotEmpty.Controls.Add(new LiteralControl("<br/>"));

            displayMinVersion = new Label();
            displayNotEmpty.Controls.Add(displayMinVersion);

            displayEmpty = new Label();
            displayEmpty.Text = "No flash movie has been uploaded";
            displayEmpty.Visible = false;
            placholder.Controls.Add(displayEmpty);
        }

        protected override void CreateEditControls(PlaceHolder placholder)
        {
            fileUpload = new FileUpload();
            fileUpload.ID = "fileUpload";
            placholder.Controls.Add(fileUpload);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            editWidthLbl = new Label();
            editWidthLbl.AssociatedControlID = "editWidth";
            editWidthLbl.Text = "Width :";
            editWidthLbl.CssClass = "formLabelShort";
            placholder.Controls.Add(editWidthLbl);

            editWidth = new TextBox();
            editWidth.ID = "editWidth";
            editWidth.MaxLength = 4;
            editWidth.Width = 25;
            placholder.Controls.Add(editWidth);

            placholder.Controls.Add(new LiteralControl("px"));

            editWidthReq = new RequiredFieldValidator();
            editWidthReq.ControlToValidate = "editWidth";
            editWidthReq.ErrorMessage = "*";
            placholder.Controls.Add(editWidthReq);

            editWidthType = new CompareValidator();
            editWidthType.ControlToValidate = "editWidth";
            editWidthType.Operator = ValidationCompareOperator.DataTypeCheck;
            editWidthType.Type = ValidationDataType.Integer;
            editWidthType.ErrorMessage = "Please enter a valid width in pixels";
            placholder.Controls.Add(editWidthType);

            placholder.Controls.Add(new LiteralControl("<br/>"));
            
            editHeightLbl = new Label();
            editHeightLbl.AssociatedControlID = "editHeight";
            editHeightLbl.Text = "Height :";
            editHeightLbl.CssClass = "formLabelShort";
            placholder.Controls.Add(editHeightLbl);

            editHeight = new TextBox();
            editHeight.ID = "editHeight";
            editHeight.MaxLength = 4;
            editHeight.Width = 25;
            placholder.Controls.Add(editHeight);

            placholder.Controls.Add(new LiteralControl("px"));

            editHeightReq = new RequiredFieldValidator();
            editHeightReq.ControlToValidate = "editHeight";
            editHeightReq.ErrorMessage = "*";
            placholder.Controls.Add(editHeightReq);

            editHeightType = new CompareValidator();
            editHeightType.ControlToValidate = "editHeight";
            editHeightType.Operator = ValidationCompareOperator.DataTypeCheck;
            editHeightType.Type = ValidationDataType.Integer;
            editHeightType.ErrorMessage = "Please enter a valid height in pixels";
            placholder.Controls.Add(editHeightType);

            editMinVersionLbl = new Label();
            editMinVersionLbl.AssociatedControlID = "editMinVersion";
            editMinVersionLbl.Text = "Min Version :";
            editMinVersionLbl.CssClass = "formLabelShort";
            placholder.Controls.Add(editMinVersionLbl);

            editMinVersion = new TextBox();
            editMinVersion.ID = "editMinVersion";
            editMinVersion.Text = "6.0.0.0";
            placholder.Controls.Add(editMinVersion);

            editMinVersionReq = new RequiredFieldValidator();
            editMinVersionReq.ControlToValidate = "editMinVersion";
            editMinVersionReq.ErrorMessage = "*";
            placholder.Controls.Add(editMinVersionReq);

        }

        protected override void DataBindChildren()
        {
            if (Properties.HasResource())
            {
                displayDimentions.Text = string.Format("{0}px x {1}px", Properties.Width, Properties.Height);
                displayLink.NavigateUrl = string.Format("~/File.aspx?Id={0}", Properties.ResourceId);
                displayMinVersion.Text = string.Format("Min Version : {0}", Properties.MinVersion);
                displayNotEmpty.Visible = true;
                displayEmpty.Visible = false;
            }
            else
            {
                displayNotEmpty.Visible = false;
                displayEmpty.Visible = true;
            }
            if(Properties.Width > 0)
                editWidth.Text = Properties.Width.ToString();
            if (Properties.Height > 0)
                editHeight.Text = Properties.Height.ToString();
            if (!string.IsNullOrEmpty(Properties.MinVersion))
                editMinVersion.Text = Properties.MinVersion;

            base.DataBindChildren();
        }

        protected override BaseContentControlProperties GetProperties()
        {
            return Properties;
        }

        protected override void SetProperties(BaseContentControlProperties properties)
        {
            this.properties = properties as FlashContentControlProperties;
        }

        protected override void SaveToProperties()
        {
            if (fileUpload.HasFile)
            {
                Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Flash, fileUpload.PostedFile, fileUpload.FileBytes);
                file.Update();
                Properties.ResourceId = file.Id;
            }
            Properties.Width = int.Parse(editWidth.Text);
            Properties.Height = int.Parse(editHeight.Text);
            Properties.MinVersion = editMinVersion.Text;
        }

        protected override void DeleteFromProperties()
        {
            if (Properties.HasResource())
            {
                Acms.Bll.File file = Acms.Bll.File.GetById(Properties.ResourceId);
                if (file != null)
                {
                    file.Delete();
                }
            }
        }
    }
}
